package com.longmao.edumsm.service.Impl;

import com.longmao.edumsm.service.MsmService;
import com.longmao.edumsm.utils.MsmPropertiesUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.stereotype.Service;

/**
 * @author longmao
 * @date 2022/2/21 10:13
 * Content:
 */
@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(String code, String phone) {
        try {
            String secretId = MsmPropertiesUtil.SECRET_ID;
            String secretKey = MsmPropertiesUtil.SECRET_KEY;
            //创建实例化认证对象
            Credential credential = new Credential(secretId, secretKey);
            //创建http选项
            HttpProfile httpProfile = new HttpProfile();
            //设置endPoint
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            //创建客户端配置对象
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            //这里的region参数一定要写,我看见帖子上好多人不写，官网也说不写就是默认参数
            SmsClient client = new SmsClient(credential, "ap-beijing", clientProfile);
            SendSmsRequest request = new SendSmsRequest();
            //创建手机号数组
            String[] phone1 = {"+86" + phone};
            //创建验证码数组
            String[] templateParamsSet1 = {code};
            request.setPhoneNumberSet(phone1);
            request.setTemplateParamSet(templateParamsSet1);
            //短信签名
            request.setSign("一二与布布个人公众号");
            //短信SdkId
            request.setSmsSdkAppid("1400566889");
            request.setTemplateID("1099092");
            SendSmsResponse sendSmsResponse = client.SendSms(request);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
